package top.lishuoboy.docker.redis.controller;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@RestController
public class DockerController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/testRedis/{count}")
    Object testRedis(@PathVariable int count) {
        //清库
        redisTemplate.delete(redisTemplate.keys("*"));

        //set数据
        for (int i = 1; i <= count; i++) {
            redisTemplate.opsForValue().set("" + i, DateUtil.now());
        }

        //get所有数据
        Map<String, String> map = new LinkedHashMap<>();
        redisTemplate.keys("*").forEach(key -> map.put(key, redisTemplate.opsForValue().get(key)));
        return map;
    }

}
